const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ProgressBarPlugin = require('progress-bar-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const isProd = process.env.NODE_ENV === 'production'

module.exports = {
  entry: path.join(__dirname, '../app/index.js'),
  output: {
    path: path.resolve(__dirname, '../dist'),
    filename: 'js/[name].[chunkhash:8].js',
    publicPath: isProd ? './' : '/',
    clean: true
  },
  cache: {
    type: 'filesystem'
  },
  optimization: {
    nodeEnv: false
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, '../app')
    },
    extensions: [
      '.js',
      '.jsx',
      '.ts',
      '.tsx',
      '.json',
      'css',
      '.sass',
      '.less'
    ],
    modules: [path.resolve(__dirname, '../node_modules')]
  },
  module: {
    rules: [
      {
        test: /.(js|jsx|ts|tsx)$/,
        use: ['thread-loader', 'babel-loader'],
        include: path.resolve(__dirname, '../app'),
        exclude: /(node_modules)/
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'cache-loader', 'css-loader', 'postcss-loader'],
        exclude: /node_modules/
      },
      {
        test: /\.less$/,
        include: [/node_modules[\\/]antd/, path.resolve('./app')],
        use: [
          'style-loader', 'cache-loader',
          {
            loader: 'css-loader',
            options: {
              importLoaders: 2,
              modules: {
                localIdentContext: path.resolve(__dirname, '../app'),
                localIdentName: '[path]__[local]',
                auto: /\.module\.\w+$/,
                exportLocalsConvention: 'camelCase'
              }
            }
          },
          'postcss-loader',
          {
            loader: 'less-loader',
            options: {
              lessOptions: {
                javascriptEnabled: true
              }
            }
          }]
      },
      {
        test: /\.html$/,
        use: {
          loader: 'html-withimg-loader',
          options: {
            esModule: false
          }
        },
        include: [path.resolve('./app/index.html')]
      },
      {
        test: /\.(png|jpe?g|gif|webp|ico|svg)$/,
        type: 'asset', // 类似 url-loader 不超过 maxSize 转为 dataURL 超过当做文件处理
        parser: {
          dataUrlCondition: {
            maxSize: 10 * 1024 // 默认是 8kb
          }
        },
        generator: {
          filename: 'static/images/[name][hash:5][ext]'
        },
        exclude: /node_modules/
      },

      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        type: 'asset/resource',
        generator: {
          filename: 'static/media/[name][hash:5][ext]'
        },
        exclude: /node_modules/
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        type: 'asset/resource',
        generator: {
          filename: 'static/font/[name][hash:5][ext]'
        }
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: path.resolve(__dirname, '../public/index.html'),
      favicon: '',
      inject: 'body',
      hash: false,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeRedundantAttributes: true,
        useShortDoctype: true,
        removeEmptyAttributes: true,
        removeStyleLinkTypeAttributes: true,
        keepClosingSlash: true,
        minifyJS: true,
        minifyCSS: true,
        minifyURLs: true
      }
    }),
    new ProgressBarPlugin(),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
    }),
    new MiniCssExtractPlugin({
      filename: 'css/[name].[contenthash:4].css'
    })
  ]
}
